{extend name="base/form_base" /}

{block name="content"}
        <form class="layui-form" id="edit_form" onsubmit="return false">
          <!-- <div class="layui-form-item">
            <label class="layui-form-label">轮播图：</label>
            <div class="layui-input-inline">
              <img src="" style="width: 100px;height: 100px" id="show_upload_img">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
              <input type="file" name="pic" onchange="upload_img(this)">
            </div>
          </div> -->
          <div class="layui-form-item">
            <label class="layui-form-label">轮播图：</label>
            <div class="layui-input-inline">
                <img src="" style="width: 100px;height: 100px" id="show_upload_img">
                <input type="hidden" name="pic" id='pic' value="">
             </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-input-block">
              <button type="button" class="layui-btn" id="upload">
                <i class="layui-icon"></i>上传图片
              </button>
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-input-block">
              <button class="layui-btn" lay-filter="formDemo" onclick="ajax()">立即提交</button>
              <button onclick="cancel()" class="layui-btn layui-btn-primary">取消</button>
            </div>
          </div>
        </form>
{/block}
{block name="script"}
<script>
      layui.use('upload', function(){
      var upload = layui.upload;
      upload.render({
      elem: '#upload',
      url:"{:url('admin/community/upload_img')}"
      // ,url: '{:C("system_url")}/api/Upload/upload_img'
      ,before:function(){
        var index = layer.msg('正在拼命处理...' , {
          icon: 16 ,
          shade: 0.6 ,
          time: 0 ,
        });
      }
      ,done: function(res){
        if(res.code==0){
          $("#show_upload_img").attr('src',res.data);
          $("#pic").val(res.data);
          layer.msg('上传图片成功');
        } else {
          layer.msg('上传图片失败');
        }
      }
      });      
    });
    function ajax(){
      var post_data = $("#edit_form").serialize();
      $.ajax({
          url:"{:url('admin/community/addSlideshow')}",
          type:"POST",
          data:post_data,
          success:function(ret){
              if (ret.code == 0) {
                layer.msg('添加成功');
                window.location.reload();
                parent.$(".layui-laypage-btn")[0].click();
              }
              if (ret.code == 1) {
                  layer.msg(ret.msg);
              }
          }
      });
    }
  //   function upload_img(el){
  //     var file = el.files[0];
  //     $("#show_upload_img").attr('src',URL.createObjectURL(file));
  //   }
  //   function ajax(){
  //     var post_data = new FormData(document.getElementById("ajaxform"));
  //     $.ajax({
  //       url:'',
  //       type:'POST',
  //       data:post_data,
  //       processData:false,
  //       contentType:false,
  //       success:function(ret){
  //         if (ret.code == 1) {
  //           window.location.href=document.referrer;
  //         } else {
  //           layer.msg(ret.msg);
  //         }
  //       }
  //     });
  //   }
    function cancel(){
    window.location.href=document.referrer;
  }
</script>
{/block}
